﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;

public class DrawRect : MonoBehaviour
{
    public Material[] rectMat;//画线的材质 不设定系统会用当前材质画线 结果不可控  
    public InputField ColorAField;//颜色透明度
    public GameObject TurnButton;//翻转按钮

    private Vector2 mMouseStart;
    private Vector2 mMouseEnd;
    private bool mBDrawMouseRect;//是否按下左键  是否画完
    private int n_Color;//选择填充颜色
    private int n_LineColor;//选择线框颜色
    private int n_Tex;//选择贴图
    private int n_Shape;//选择形状
    private int e_Scale = 1;//橡皮粗细
    private float n_Scale = 0;//缩放比例1
    private float TempVector = 0;//翻转
    private Color BGColor;//背景颜色
    private Color LEColor;//线框颜色
    private bool isDraw = true;//判断现在是否需要绘制图形
    private bool isErase = false;//判断是否使用了擦除功能
    private bool ChangeColor = false;//判断是否启用渐变
    private bool isTurn = false;//判断是否翻转
    private List<Vector3> lineInfo = new List<Vector3>();

    void Start()
    {
        mBDrawMouseRect = false;
        n_Color = 0;
        n_Tex = 0;
        n_Shape = 0;
        ColorAField.text = "0.5f";
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0) && isDraw)//按下鼠标左键并且开始绘制
        {
            Vector3 mousePosition = Input.mousePosition;
            mMouseStart = new Vector2(mousePosition.x, mousePosition.y);

        }
        if (Input.GetMouseButton(0) && isDraw)//持续按下鼠标左键  
        {
            mBDrawMouseRect = true;
            Vector3 mousePosition = Input.mousePosition;
            mMouseEnd = new Vector2(mousePosition.x, mousePosition.y);
        }
        if (Input.GetMouseButtonUp(0) && mBDrawMouseRect)
        {
            isDraw = false;
            if (EventSystem.current.IsPointerOverGameObject())
            {
                Debug.Log("当前触摸在UI上" + EventSystem.current.currentSelectedGameObject.name);
                if (EventSystem.current.currentSelectedGameObject.name == "ReDraw" ||
                    EventSystem.current.currentSelectedGameObject.name == "Triangle" ||
                    EventSystem.current.currentSelectedGameObject.name == "Cube" ||
                    EventSystem.current.currentSelectedGameObject.name == "Trapezoid")//如果鼠标抬起的位置是这三个Button，就可以再绘制一次
                {
                    isDraw = true;
                }
            }
            else
            {
                Debug.Log("当前没有触摸在UI上");
            }
        }
        if (isErase && Input.GetMouseButton(0))
        {
            lineInfo.Add(Input.mousePosition);
        }
        Debug.Log("isDraw=" + isDraw);
        Debug.Log("isErase=" + isErase);
        Debug.Log("ChangeColor=" + ChangeColor);
    }

    void OnGUI()
    {
        if (isErase)
        {
            rectMat[3].SetPass(0);
            GL.LoadOrtho();
            GL.Begin(GL.LINES);
            for (int i = 0; i < lineInfo.Count - 1; i++)
            {
                Vector3 start = lineInfo[i];
                Vector3 end = lineInfo[i + 1];
                for (int k = -e_Scale; k <= e_Scale; k++)
                {
                    DrawErase(start.x + k, start.y + k, end.x + k, end.y + k);
                }
            }
            GL.End();
        }
        if (mBDrawMouseRect)
        {
            switch (n_Shape)
            {
                case 0:
                    DrawCube(mMouseStart, mMouseEnd);
                    break;
                case 1:
                    DrawTriangle(mMouseStart, mMouseEnd);
                    break;
                case 2:
                    DrawTrapezoid(mMouseStart, mMouseEnd);
                    break;
            }
        }
    }

    private void DrawErase(float x1, float y1, float x2, float y2)//折线绘制
    {
        GL.Vertex(new Vector3(x1 / Screen.width, y1 / Screen.height, 0));
        GL.Vertex(new Vector3(x2 / Screen.width, y2 / Screen.height, 0));
    }

    void DrawCube(Vector2 start, Vector2 end)//方形
    {
        rectMat[n_Tex].SetPass(0);//设置材质通道
        GL.PushMatrix();//保存摄像机变换矩阵  

        Color bgclr = BGColor;//填充颜色设置
        bgclr.a = float.Parse(ColorAField.text);
        bgclr.a = Mathf.Clamp(bgclr.a, 0, 1);

        Color leclr = LEColor;//线框颜色设置

        if (isTurn)
        {
            TempVector = 2 * (start.y - end.y);//翻转值
        }
        else
        {
            TempVector = 0;
        }

        GL.LoadPixelMatrix();//设置用屏幕坐标绘图  
                             //透明框  
        GL.Begin(GL.QUADS);
        GL.Color(bgclr);//矩形填充颜色
        if (ChangeColor) GL.Color(Color.yellow);//判断是否使用颜色渐变
        GL.TexCoord(new Vector3(0, 1, 0));
        GL.Vertex3(start.x - n_Scale, start.y - TempVector + n_Scale, 0);//左上
        if (ChangeColor) GL.Color(Color.red);
        GL.TexCoord(new Vector3(1, 1, 0));
        GL.Vertex3(end.x + n_Scale, start.y - TempVector + n_Scale, 0);//右上
        if (ChangeColor) GL.Color(Color.blue);
        GL.TexCoord(new Vector3(1, 0, 0));
        GL.Vertex3(end.x + n_Scale, end.y - n_Scale, 0);//右下
        if (ChangeColor) GL.Color(Color.green);
        GL.TexCoord(new Vector3(0, 0, 0));//左下
        GL.Vertex3(start.x - n_Scale, end.y - n_Scale, 0);
        GL.End();

        //线  
        //上  
        GL.Begin(GL.LINES);
        GL.Color(leclr);
        GL.Vertex3(start.x - n_Scale, start.y - TempVector + n_Scale, 0);
        GL.Vertex3(end.x + n_Scale, start.y - TempVector + n_Scale, 0);
        GL.End();

        //下  
        GL.Begin(GL.LINES);
        GL.Color(leclr);
        GL.Vertex3(start.x - n_Scale, end.y - n_Scale, 0);
        GL.Vertex3(end.x + n_Scale, end.y - n_Scale, 0);
        GL.End();

        //左  
        GL.Begin(GL.LINES);
        GL.Color(leclr);
        GL.Vertex3(start.x - n_Scale, start.y - TempVector + n_Scale, 0);
        GL.Vertex3(start.x - n_Scale, end.y - n_Scale, 0);
        GL.End();

        //右  
        GL.Begin(GL.LINES);
        GL.Color(leclr);
        GL.Vertex3(end.x + n_Scale, start.y - TempVector + n_Scale, 0);
        GL.Vertex3(end.x + n_Scale, end.y - n_Scale, 0);
        GL.End();

        GL.PopMatrix();//还原  
    }

    void DrawTriangle(Vector2 start, Vector2 end)//三角形
    {
        rectMat[n_Tex].SetPass(0);
        GL.PushMatrix();//保存摄像机变换矩阵  

        Color clr = BGColor;//填充颜色设置
        clr.a = float.Parse(ColorAField.text);
        clr.a = Mathf.Clamp(clr.a, 0, 1);

        Color leclr = LEColor;//线框颜色设置

        if (isTurn)
        {
            TempVector = 2 * (start.y - end.y);//翻转值
        }
        else
        {
            TempVector = 0;
        }

        GL.LoadPixelMatrix();//设置用屏幕坐标绘图  
                             //透明框  
        GL.Begin(GL.TRIANGLES);
        GL.Color(clr);//矩形填充颜色
        if (ChangeColor) GL.Color(Color.red);
        GL.TexCoord(new Vector3(0.5f, 1, 0));
        GL.Vertex3(start.x, start.y - TempVector + n_Scale, 0);//顶点
        if (ChangeColor) GL.Color(Color.green);
        GL.TexCoord(new Vector3(1, 0, 0));
        GL.Vertex3(end.x + n_Scale, end.y - n_Scale, 0);//右下
        if (ChangeColor) GL.Color(Color.blue);
        GL.TexCoord(new Vector3(0, 0, 0));
        GL.Vertex3(2 * start.x - end.x - n_Scale, end.y - n_Scale, 0);//左下
        GL.End();

        //线框 左  
        GL.Begin(GL.LINES);
        GL.Color(leclr);
        GL.Vertex3(start.x, start.y - TempVector + n_Scale, 0);
        GL.Vertex3(2 * start.x - end.x - n_Scale, end.y - n_Scale, 0);
        GL.End();

        //右 
        GL.Begin(GL.LINES);
        GL.Color(leclr);
        GL.Vertex3(start.x, start.y - TempVector + n_Scale, 0);
        GL.Vertex3(end.x + n_Scale, end.y - n_Scale, 0);
        GL.End();

        //底  
        GL.Begin(GL.LINES);
        GL.Color(leclr);
        GL.Vertex3(2 * start.x - end.x - n_Scale, end.y - n_Scale, 0);
        GL.Vertex3(end.x + n_Scale, end.y - n_Scale, 0);
        GL.End();

        GL.PopMatrix();//还原  
    }

    void DrawTrapezoid(Vector2 start, Vector2 end)//多边形
    {
        rectMat[n_Tex].SetPass(0);
        GL.PushMatrix();//保存摄像机变换矩阵  

        Color clr = BGColor;//填充颜色设置
        clr.a = float.Parse(ColorAField.text);
        clr.a = Mathf.Clamp(clr.a, 0, 1);

        Color leclr = LEColor;//线框颜色设置

        if (isTurn)
        {
            TempVector = 2 * (start.y - end.y);//翻转值
        }
        else
        {
            TempVector = 0;
        }

        GL.LoadPixelMatrix();//设置用屏幕坐标绘图  
                             //透明框  
        GL.Begin(GL.TRIANGLE_STRIP);
        GL.Color(clr);//矩形填充颜色
        if (ChangeColor) GL.Color(Color.yellow);
        GL.TexCoord(new Vector3(0, 1, 0));
        GL.Vertex3(start.x, start.y - TempVector, 0);//左上
        if (ChangeColor) GL.Color(Color.red);
        GL.TexCoord(new Vector3(1, 1, 0));
        GL.Vertex3(end.x - 150, start.y - TempVector, 0);//右上
        if (ChangeColor) GL.Color(Color.green);
        GL.TexCoord(new Vector3(0, 0, 0));
        GL.Vertex3(start.x - 150, end.y, 0);//右下
        if (ChangeColor) GL.Color(Color.blue);
        GL.TexCoord(new Vector3(1, 0, 0));
        GL.Vertex3(end.x, end.y, 0);//左下

        GL.End();

        //线框 上  
        GL.Begin(GL.LINES);
        GL.Color(leclr);
        GL.Vertex3(start.x, start.y - TempVector, 0);
        GL.Vertex3(end.x - 150, start.y - TempVector, 0);
        GL.End();

        //左 
        GL.Begin(GL.LINES);
        GL.Color(leclr);
        GL.Vertex3(start.x, start.y + -TempVector, 0);
        GL.Vertex3(start.x - 150, end.y, 0);
        GL.End();

        //底  
        GL.Begin(GL.LINES);
        GL.Color(leclr);
        GL.Vertex3(start.x - 150, end.y, 0);
        GL.Vertex3(end.x, end.y, 0);
        GL.End();

        //右
        GL.Begin(GL.LINES);
        GL.Color(leclr);
        GL.Vertex3(end.x - 150, start.y + -TempVector, 0);
        GL.Vertex3(end.x, end.y, 0);
        GL.End();

        GL.PopMatrix();//还原  
    }

    public void ChooseColor(int i)//选择填充颜色
    {
        n_Color = i;
        switch (i)
        {
            case 0:
                BGColor = Color.black;
                ChangeColor = false;
                n_Tex = 0;
                break;
            case 1:
                BGColor = Color.white;
                ChangeColor = false;
                n_Tex = 0;
                break;
            case 2:
                BGColor = Color.red;
                ChangeColor = false;
                n_Tex = 0;
                break;
            case 3:
                BGColor = Color.green;
                ChangeColor = false;
                n_Tex = 0;
                break;
            case 4:
                BGColor = Color.blue;
                ChangeColor = false;
                n_Tex = 0;
                break;
            case 5:
                BGColor = Color.yellow;
                ChangeColor = false;
                n_Tex = 0;
                break;
            case 6:
                ChangeColor = true;//渐变颜色
                n_Tex = 0;
                break;
            default:
                BGColor = Color.black;
                ChangeColor = false;
                n_Tex = 0;
                break;
        }
    }

    public void ChooseLineColor(int i)//选择线框颜色
    {
        n_LineColor = i;
        switch (i)
        {
            case 0:
                LEColor = Color.black;
                break;
            case 1:
                LEColor = Color.white;
                break;
            case 2:
                LEColor = Color.red;
                break;
            case 3:
                LEColor = Color.green;
                break;
            case 4:
                LEColor = Color.blue;
                break;
            case 5:
                LEColor = Color.yellow;
                break;
            case 6:
                LEColor = Color.gray;
                break;
            default:
                LEColor = Color.black;
                break;
        }
    }

    public void ChooseTex(int i)//选择贴图
    {
        n_Tex = i;
        ChangeColor = false;
    }

    public void ChooseShape(int i)//选择形状
    {
        mBDrawMouseRect = false;
        n_Shape = i;
        isDraw = true;
        isErase = false;
        ChangeColor = false;
    }

    public void ReDraw()//重画
    {
        mBDrawMouseRect = false;
        isDraw = true;
        isErase = false;
        lineInfo.Clear();
        ChangeColor = false;
    }

    public void Erase()//擦除
    {
        isErase = true;
    }

    public void EraseSize(int i)//橡皮粗细
    {
        e_Scale = i;
        lineInfo.Clear();
    }

    public void ShapeScale(int i)//缩放
    {
        n_Scale = i;
    }

    public void OverTurn()//翻转
    {
        if (isTurn)
        {
            isTurn = false;
            TurnButton.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
        }
        else
        {
            isTurn = true;
            TurnButton.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 180));
        }
    }

    public void Exit()
    {
        Application.Quit();
    }
}